package roborally.filters;

import roborally.model.Element;
import roborally.model.EnergyAmount;
import roborally.model.IEnergyHolder;

/**
 * A class to check whether a given object satisfies the condition
 * of having a certain minimal amount of energy.
 * 
 * @invar	The given energy amount should be a valid amount.	
 * 
 * @version 1.0
 * @author  Pieter Robberechts	(1e Bach Informatica)
 * @author	Jeroen Sanders		(1e Bach Informatica)
 */
public class EnergyAtLeastEvaluator extends ConditionEvaluator {

	/**
	 * Initialize this new energy-at-least evaluator with 
	 * a minimal energy amount.
	 * 
	 * @param 	energy
	 * 			The minimal energy amount necessary to satisfy this condition.
	 * @pre		The given energy amount should be a valid amount.
	 * @post	The given energy amount is set as the energy amount for this 
	 * 			energy-at-least-evaluator.
	 */
	public EnergyAtLeastEvaluator(EnergyAmount energy) {
		assert canHaveAsEnergy(energy);
		this.energy = energy;
	}
	
	/**
	 * Return the minimal energy amount necessary to satisfy this condition.
	 */
	public EnergyAmount getEnergy() {
		return energy;
	}
	
	/**
	 * Check whether the given energy amount is a valid amount for this
	 * energy-at-least evaluator.
	 * 
	 * @param 	energy
	 * 			The amount of energy to check for whether it is a valid one.
	 * @return	Return true if and only if the given energy amount is effective;
	 * 			else return false.
	 */
	public boolean canHaveAsEnergy(EnergyAmount energy) {
		return ( energy != null );
	}

	/**
	 * Variable registering the minimal amount of energy 
	 * necessary to satisfy this condition.
	 */
	private final EnergyAmount energy;
	
	/**
	 * Check whether the given object satisfies this energy-at-least condition.
	 * 
	 * @return	Return false if the given object isn't an energy holder.
	 * @return	Return true if and only if the given object is an energy holder
	 * 			and has an energy amount which is not less than the energy amount
	 * 			associated with this energy-at-least-evaluator; else return false.
	 */
	@Override
	public boolean isTrue(Element object) {
		if(object == null)
			return false;
		if(!(object instanceof IEnergyHolder))
			return false;
		return ! ((IEnergyHolder) object).getEnergy().lessThan(getEnergy());
	}

}
